import {
    createSSRApp
} from "vue";
// main.js
import App from "./App.vue";
import Http from '@/utils/http'
import pinia from '@/store'
import Validate from "@/utils/formValidate";
import DayJs from 'dayjs';
import {globalDirective} from '@/utils/directives'
import cal from '@/utils/cal'
import {debounce,throttle} from '@/utils/axiosSetTime'
// 计算自定义高度容器流出
import TopHeight from "@/common/topHeight"
// 注册公共组件
import Header from "@/componentsTool/header/index.vue";
import HeaderSearch from "@/componentsTool/header/headerSearch.vue";
import Tabbar from "@/componentsTool/tabbar/index.vue";
import Tabs from '@/componentsTool/tabs/tabs.vue'
import DaTree from '@/componentsTool/da-tree/index'
import SpeechRecognition from '@/componentsTool/voice/speechRecognition'
import fileView from '@/componentsTool/fileView/index'
import fileUpload from '@/componentsTool/fileUpload/index'
import drawSign from '@/componentsTool/draw-sign/index'

//公用样式文件
import "@/static/css/packageBase/smain.scss";
import "@/static/css/packageBase/lmain.scss";
//日常检查样式
import "@/static/css/dailycheck/dailycheck.scss";

export function createApp() {
    const app = createSSRApp(App);
    app.config.globalProperties.http = Http;
    app.config.globalProperties.topHeight = TopHeight;
    app.config.globalProperties.validate = Validate;
    app.config.globalProperties.dayjs = DayJs;
    app.config.globalProperties.store = pinia.store;
    app.use(pinia.init);
    app.config.globalProperties.globalDirective = globalDirective;
    app.config.globalProperties.cal = cal;
    app.config.globalProperties.axiosSetTime = {debounce,throttle};
    app.component('Header', Header);
    app.component('HeaderSearch', HeaderSearch);
    app.component('Tabbar', Tabbar);
    app.component('Tabs', Tabs);
    app.component('DaTree', DaTree);
    app.component('SpeechRecognition', SpeechRecognition);
    app.component('fileView', fileView);
    app.component('fileUpload', fileUpload);
    app.component('drawSign', drawSign);
    app.directive('hasPermission', {
        mounted(el, binding, VNode, prevNode) {
            const {
                value
            } = binding
            const all_permission = "*:*:*";
            const permissions = pinia.store().userStore.permissions;
            console.log(permissions)
            if (value && value instanceof Array && value.length > 0) {
                const permissionFlag = value;
                console.log(value)
                const hasPermissions = permissions.some(permission => {
                    return all_permission === permission || permissionFlag.includes(permission)
                })
                if (!hasPermissions) {
                    el.parentNode && el.parentNode.removeChild(el)
                }
            } else {
                throw new Error(`请设置操作权限标签值`)
            }
        },
    })
    return {
        app,
    };
}
